<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 声明变量存储用户输入的值,使用 '+' 正号隐式转换为number;num2 存储累加和
        let num = +prompt('请输入一个数值'), num2 = 0;
        // for循环打印出来用户输出的值
        for (let i = 1; i <= num; i++) {
            console.log(i);
            // 每次打印的i 赋值相加给 num2
            num2 += i;
        };
        document.write(num2);
    </script>
</body>

</html>